using System.Data;
using System.Web.UI.WebControls;
using Butterfly.Library.Components.Modules;
using Butterfly.Library.Components.Providers;
using Butterfly.Library.Utilities;

namespace Butterfly.Library.Components.Tabs
{
	public class TabController
	{
		public const string key = "Butterfly.Portal.Tab";
		public static DataTable GetTabs()
		{
			string param = "GetTabs?lang=" + AppEnv.GetLanguage();
			DataCaching data = new DataCaching();
			DataTable dtTabs = (DataTable)data.GetHashCache(key, param);
			if (dtTabs != null) return dtTabs;
			dtTabs = DataAccessProvider.Instance().GetTabs(AppEnv.GetLanguage());
			data.SetHashCache(key, param, AppEnv.GetTimeCacheExpire(),  dtTabs);
			return dtTabs;
		}
        public static DataTable GetTabsByPortalID(int portalID)
        {
            DataTable dtTabs = DataAccessProvider.Instance().GetTabsByPortalID(portalID,AppEnv.GetLanguage());           
            return dtTabs;
        }
		public static DataTable GetTabs(int parent)
		{
			string param = "GetTabs?parent=" + parent.ToString();
			DataCaching data = new DataCaching();
			DataTable dtTabs = (DataTable)data.GetHashCache(key, param);
			if (dtTabs != null) return dtTabs;
			dtTabs = DataAccessProvider.Instance().GetTabs(parent);
			data.SetHashCache(key, param, AppEnv.GetTimeCacheExpire(), dtTabs);
			return dtTabs;
		}
		public static DataTable GetTabs(bool visible, bool enable, string lang)
		{
			string param = "GetTabs?visible=" + visible.ToString() + "&enable=" + enable.ToString() + "lang=" + lang;
			DataCaching data = new DataCaching();
			DataTable dtTabs = (DataTable)data.GetHashCache(key, param);
			if (dtTabs != null) return dtTabs;
			dtTabs = DataAccessProvider.Instance().GetTabs(visible, enable, lang);
			data.SetHashCache(key, param, AppEnv.GetTimeCacheExpire(), dtTabs);
			return dtTabs;
		}
		public static int AddTab(TabInfo tab)
		{
			int retval = DataAccessProvider.Instance().AddTab(tab);
			DataCaching dc = new DataCaching();
			dc.RemoveCache(key);
			return retval;
		}
		public static void UpdateTab(TabInfo tab)
		{
			DataAccessProvider.Instance().UpdateTab(tab);
			DataCaching dc = new DataCaching();
			dc.RemoveCache(key);
		}
		public static void DeleteTab(int tabId)
		{
			DataAccessProvider.Instance().DeleteTab(tabId);
			DataCaching dc = new DataCaching();
			dc.RemoveCache(key);
		}
		public static TabInfo GetTab(int tabId)
		{
			return DataAccessProvider.Instance().GetTab(tabId);
		}
		public static TabControlInfo GetTabControl(int tabControlId)
		{
			return DataAccessProvider.Instance().GetTabControl(tabControlId);
		}
		public static TabSetting GetTabSettings(int tabId)
		{
			string param = "GetTabSettings?tabid=" + tabId.ToString();
			DataCaching data = new DataCaching();
			TabSetting setting = (TabSetting)data.GetHashCache(key, param);
			if (setting != null) return setting;
			setting = DataAccessProvider.Instance().GetTabSettings(tabId);
			data.SetHashCache(key, param, AppEnv.GetTimeCacheExpire(), setting);
			return setting;
		}
		public static void FillToList(ListItemCollection lstItems, DataTable source)
		{
			DataRow[] drRoots = source.Select("TabParentID = 0");
			foreach(DataRow row in drRoots)
			{
				ListItem item = new ListItem(row["TabName"].ToString(), row["TabID"].ToString());
				item.Attributes.Add("Level","0");
				lstItems.Add(item);
				LoadForCurItem(lstItems, item, source);
			}
		}
		private static void LoadForCurItem(ListItemCollection lstItems, ListItem curItem, DataTable source)
		{
			int curTabId = ConvertUtility.ToInt32(curItem.Value);
			int level = ConvertUtility.ToInt32(curItem.Attributes["Level"]);
			level++;
			DataRow[] drChilds = source.Select("TabParentID = " + curTabId);
			foreach(DataRow row in drChilds)
			{
				ListItem item = new ListItem( MiscUtility.StringIndent(level) + row["TabName"].ToString(), row["TabID"].ToString());
				item.Attributes.Add("Level", level.ToString());
				lstItems.Add(item);
				LoadForCurItem(lstItems, item, source);
			}
		}
		public static void AddControlToTab(int tabControlId, int tabId, int controlId, string layoutPosition, int order)
		{
			
			DataAccessProvider.Instance().AddControlToTab(tabControlId, tabId, controlId, layoutPosition, order);
			DataCaching data = new DataCaching();
			data.RemoveCache(ModuleControlController.key);
		}
		public static void UpdateTabControl(int tabControlId, int tabId, string layoutPosition, int order)
		{
			DataAccessProvider.Instance().UpdateTabControl(tabControlId, tabId, layoutPosition, order);
			DataCaching data = new DataCaching();
			data.RemoveCache(ModuleControlController.key);
		}
		public static void RemoveControlFromTab(int tabControlId)
		{
			DataAccessProvider.Instance().RemoveControlFromTab(tabControlId);
			DataCaching data = new DataCaching();
			data.RemoveCache(ModuleControlController.key);
		}
	}
}
